<?php



if (!empty($_GET["action"])) {
    switch ($_GET["action"])
    {
    case 'listar':
        listar();        
        break;
    case 'buscar':
        buscar();
        break;
    case 'leido':
        leido();
       break;
   case 'delete':       
       break;
    case 'crearEvento':
        crearEvento();
        break;
    case 'listarParticioantes':
        listarParticioantes();
        break;
    case 'agregarParticioante':
        agregarParticioante();
       break;
    case 'buscarPersona':
        buscarPersona();
        break;
    case 'eliminarParticipante':
        eliminarParticipante();
        break;
    default :
        echo $_GET["action"];
        break;
    }
}

function listar() {
    try {
        include_once './conexion2.php';
        session_start();
        $conexion = open();
        $query = $conexion->prepare("CALL sp_eventos (?)");
        $query->bindParam(1, $_SESSION["Cedula"]);        
        $query->execute();                                 
        $fila = $query->fetchAll(PDO::FETCH_NAMED);               
        echo json_encode($fila);        
        $conection=close();        
    } catch (PDOException $error) {
        echo $error->getMessage();
    }
}

function buscar() {
   try {
        include_once './conexion2.php';
        $conexion = open();
        $query = $conexion->prepare("CALL sp_evento (?)");
        $query->bindParam(1, $_POST["id"]);
        $query->execute();                 
        $fila = $query->fetch(PDO::FETCH_NAMED);
        echo json_encode($fila);        
        $conection=close();        
    } catch (PDOException $error) {
        echo $error->getMessage();
    }
}

function leido() {
    try {
        include_once './conexion2.php';
        session_start();
        $conexion = open();
        $query = $conexion->prepare("CALL sp_eventoLeido (?,?)");
        $query->bindParam(1, $_POST["id"]);        
        $query->bindParam(2, $_SESSION["Cedula"]);        
        $query->execute();                                        
        $conexion=close();        
    } catch (PDOException $error) {
        echo $error->getMessage();
    }
}

function crearEvento() {            
    try {                
      include_once './conexion2.php';
      session_start();
      $conexion = open();
      $query = $conexion->prepare("CALL `sp_crearEvento`(?, ?, ?, ?, ?, ?, ?, ?)");
      $query->bindValue(1, $_SESSION["Cedula"]);
      $query->bindValue(2, $_POST["Asunto"]);
      $query->bindValue(3, $_POST["Lugar"]);
      $query->bindValue(4, $_POST["FechaInicio"].' '.$_POST["HoraInicio"]);
      $query->bindValue(5, $_POST["FechaFinal"].' '.$_POST["HoraFinal"]);
      $query->bindValue(6, $_POST["TipoEvento"]);
      $query->bindValue(7, $_FILES['Anexo']['name']);
      $query->bindValue(8, $_POST["DefinirHora"], PDO::PARAM_BOOL);    
      $query->bindColumn(1, $id, PDO::PARAM_INT);        
      $query->execute();
      $query->fetch(PDO::FETCH_BOUND);      
      move_uploaded_file($_FILES['Anexo']['tmp_name'], '../anexos/'.$id);
      $conexion = close();
      echo $id;
      
    } catch (PDOException $error) {
      echo $error->getMessage();
    }
    
    //$tipo_archivo = split('/', $_FILES['Anexo']['type']);
    //echo $tipo_archivo[1];                
}

function agregarParticioante() {            
    try {                
      include_once './conexion2.php';
      session_start();
      $conexion = open();
      $query = $conexion->prepare("CALL `sp_agregarParticipante`(?, ?)");
      $query->bindValue(1, $_GET["id"]);
      $query->bindValue(2, $_GET["Cedula"]);      
      $query->execute();            
      $conexion = close();
    } catch (PDOException $error) {
      echo $error->getMessage();      
    }     
}

function listarParticioantes() {            
    try {                
      include_once './conexion2.php';
      session_start();
      $conexion = open();
      $query = $conexion->prepare("CALL `sp_listarParticipantes`(?)");
      $query->bindValue(1, $_GET["id"]);      
      $query->execute();            
      $fila = $query->fetchAll(PDO::FETCH_NUM);               
      echo json_encode(array("aaData"=>$fila));        
      $conexion = close();
      
    } catch (PDOException $error) {
      echo $error->getMessage();
    }                 
}

function buscarPersona() {
    try {                
      include_once './conexion2.php';
      session_start();
      $conexion = open();
      $query = $conexion->prepare("CALL `sp_buscarPersona`(?)");
      $query->bindValue(1, $_GET['term']);      
      $query->execute();            
      $fila = $query->fetchAll(PDO::FETCH_NAMED);               
      echo json_encode($fila);
      $conexion = close();
      
    } catch (PDOException $error) {
      echo $error->getMessage();
    }    
}

function eliminarParticipante() {
      try {                
      include_once './conexion2.php';
      session_start();
      $conexion = open();
      $query = $conexion->prepare("CALL `sp_eliminarParticipante`(?,?)");
      $query->bindValue(1, $_GET['id']);      
      $query->bindValue(2, $_GET['Cedula']);      
      $query->execute();                  
      $conexion = close();
      
    } catch (PDOException $error) {
      echo $error->getMessage();
    } 
}

?>
